home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1996 September
/
MACPOWER-1996-09.ISO.7z
/
MACPOWER-1996-09.ISO
/
第2特集:プラグイン大集合
/
PIC2 Save
/
PIC2Main.c
< prev
next >
Wrap
Text File
|
1995-01-16
|
9KB
|
440 lines
/*
File: PIC2Main.c
Copyright © 1995 K&M Software Corporation (番犬 JBE01713@niftyserve.or.jp)
Portions © 1992 Adobe Systems Incorporated
Apple Computer, Inc.
Free Software Foundation, Inc.
Pi Format © やなぎさわ
Version History:
1.0 First Create!
*/
/*****************************************************************************/
/* definitions */
#ifdef __MWERKS__
#include <A4Stuff.h>
#endif
#include "pic2.h"
#include "PIC2PlugIn.h"
/*****************************************************************************/
/* Sets the global variables to their default values. */
void InitGlobals(GHdl globals)
{
// Clear globals
memset((Ptr) *globals, 0, sizeof(Globals));
}
/*****************************************************************************/
/* Prepare to acquire an image. If the plug-in module needs only a limited
amount of memory, it can lower the value of the 'maxData' field. */
void DoPrepare(GHdl globals)
{
gResult = noErr;
gStuff->maxData = gStuff->maxData*2/3L;
#if 0
if (gStuff->maxData > 1524L*1024L) /* 1.5MByte */
gStuff->maxData = 1524L*1024L; /* 1.5MByte */
else
gResult = memFullErr; // Sorry..
#endif
}
/*****************************************************************************/
#define kOKOutlineItem 3
#define k24bitColor 4
#define k15bitColor 5
#define kBetaMode 6
#define kFastMode 7
#define kCompressMode 8
#define kTitleStr 9
#define kCommentStr 10
#define kAuthorStr 11
#define kPicIndexStr 12
#define inkey(keys,mask) ( ((Ptr)keys)[(mask>>3)] & (1<<(mask&7)) )
Boolean GetParameters(GHdl globals)
{
short i,item,itemType;
Rect r;
Point pt;
SFReply reply;
ControlHandle ch1[8];
Handle strs[4];
Str255 str,str1;
DialogTHndl dt;
DialogPtr dp;
(**globals).p2 = nil;
SetPt(&pt,100,100);
(**globals).compressMode = 2;
dt = (DialogTHndl) GetResource ('DLOG', kMainDialogID);
HNoPurge ((Handle) dt);
CenterDialog (dt);
dp = GetNewDialog (kMainDialogID, nil, (WindowPtr) -1);
ShowWindow(dp);
SetPort(dp);
SetOutlineOKHook(dp, kOKOutlineItem);
ParamText((StringPtr)cPIC2SaveVersion,(StringPtr)nil,
(StringPtr)nil,(StringPtr)nil);
SelectDialogItemText(dp, kTitleStr, 0, 255);
for(i=0;i<2;i++)
{
GetDialogItem (dp, k24bitColor+i, &itemType, (Handle *) &ch1[i], &r);
if(gStuff->imageMode == exportModeIndexedColor)
HiliteControl(ch1[i], 255); // inactive state
else
HiliteControl(ch1[i], 0);
}
for(i=0;i<3;i++)
GetDialogItem (dp, kBetaMode+i, &itemType, (Handle *) &ch1[i+2], &r);
for(i=0;i<4;i++)
GetDialogItem (dp, kTitleStr+i, &itemType, (Handle *) &strs[i], &r);
do
{
if(gStuff->imageMode != exportModeIndexedColor)
{
for(i=0;i<2;i++)
SetControlValue (ch1[i], (**globals).colorMode==i);
}
for(i=0;i<3;i++)
SetControlValue (ch1[i+2], (**globals).compressMode==i);
ModalDialog (nil, &item);
switch(item)
{
case k24bitColor:
case k24bitColor+1:
(**globals).colorMode = item-k24bitColor;
break;
case kBetaMode:
case kBetaMode+1:
case kBetaMode+2:
(**globals).compressMode = item-kBetaMode;
break;
}
}
while (item != ok && item != cancel);
if(item==ok)
{
Str255 p;
long count;
// Dialogs.h
GetDialogItemText(strs[0],p);
if(StrLength(p)<30)
{
BlockMove(&p[1],(**globals).title,StrLength(p));
(**globals).title[StrLength(p)]=0;
}
GetDialogItemText(strs[1],p);
BlockMove(&p[1],(**globals).comment,StrLength(p));
(**globals).comment[StrLength(p)]=0;
GetDialogItemText(strs[2],p);
if(StrLength(p)<17)
{
BlockMove(&p[1],(**globals).author,StrLength(p));
(**globals).author[StrLength(p)]=0;
}
GetDialogItemText(strs[3],p);
StringToNum(p, &count);
(**globals).picindex = count;
}
DisposDialog (dp);
HPurge ((Handle) dt);
if(item == cancel)
return FALSE;
GetIndString(str, kStringID, kStrSavePromptItem);
BlockMove(gStuff->filename,str1,250);
str1[1+str1[0]]='.';
str1[0]++;
for(item=1;item<=str1[0];item++)
if(str1[item]=='.') // not appear in SJIS-2byte
{ BlockMove(".p2",&str1[item],4);
break;
}
for(item=0;str1[1+item]!=0 && item<30;) item++;
str1[0]=item;
SFPutFile(pt,str,str1,nil,&reply);
if(!reply.good) return FALSE;
BlockMove(reply.fName,gStuff->filename,reply.fName[0]+1);
gStuff->vRefNum = reply.vRefNum;
gStuff->dirty = FALSE;
ReplyToSpec(&(**globals).theSpec, &reply);
return TRUE;
}
#undef kMainDialogID
/*****************************************************************************/
/* Asks the user and the returns to the calling program the image parmaters. */
void DoStart(GHdl globals)
{
P2* p2;
if(gResult != noErr) return;
if (gStuff->imageMode != exportModeRGBColor && gStuff->imageMode != exportModeIndexedColor)
{
gResult = exportBadMode;
return;
}
if (!GetParameters(globals))
{
errorexit:;
gResult = 1;
return;
}
{
Str255 str;
short c;
GetIndString(str, kStringID, kStrSaverItem);
str[1+str[0]]=0;
if(gStuff->imageMode == exportModeRGBColor)
c = ((**globals).colorMode==1)? 15 : 24;
else
c = 8;
(**globals).BitsDepth = c;
p2setenv((char*)&str[1], c, -1, -1, 1, 1, 0);
if(c==8)
{
uchar pal[256][3];
short i;
for(i=0;i<256;i++)
{
pal[i][0] = gStuff->redLUT[i];
pal[i][1] = gStuff->greenLUT[i];
pal[i][2] = gStuff->blueLUT[i];
}
p2setpal(256,8,pal);
}
p2 = p2creat(&(**globals).theSpec, (**globals).title, (**globals).comment,
(**globals).author, (**globals).picindex);
if(p2==nil)
{
gResult = DoP2Error(globals);
return;
}
}
gResult = noErr;
{
char mode[20];
// Create Additional Tag: 'PDPI'
write_file(p2, "PDPI", 4);
write_long(p2, 10); // 10byte
write_short(p2, FixRound(gStuff->imageHRes));
/* 次にセーブするブロック位置をせっと */
p2->next_pos = tell_file( p2);
mode[4]=0;
mode[0]='P';
mode[1]='2';
(**globals).partial = 0;
(**globals).total = gStuff->imageSize.v;
switch((**globals).compressMode)
{
case 0:
mode[2]='B';
mode[3]='M';
break;
case 1:
mode[2]='S';
mode[3]='F';
break;
default:
mode[2]='S';
mode[3]='S';
(**globals).total *= 2;
break;
}
(**globals).cLine = p2save(p2, &(**globals).Pixel,
0, 0,
gStuff->imageSize.h, gStuff->imageSize.v,
mode, 0xffffffffL);
}
(**globals).p2 = p2;
(**globals).buffer_end = FALSE;
gStuff->theRect.left = 0;
gStuff->theRect.right = gStuff->imageSize.h;
gStuff->theRect.top = (**globals).cLine;
gStuff->theRect.bottom = (**globals).cLine+1;
gStuff->loPlane = 0;
gStuff->hiPlane = gStuff->planes - 1; // 3 planes
gStuff->dirty = FALSE;
}
/*****************************************************************************/
/* Returns the next hunk of the image to the calling program. */
void DoContinue(GHdl globals)
{
short cLine;
register Byte* p;
register pix *col;
short i,width;
/* Since Photoshop will continue to call DoContinue until it
returns error or data=NIL, we have to tell him its end. */
if(gResult || (**globals).buffer_end)
{
eexit:;
gStuff->data = NULL;
return;
}
width = gStuff->imageSize.h;
col = (**globals).Pixel;
cLine = (**globals).cLine;
p = (Byte*) (gStuff->data);
if((cLine & 63)==0)
{
if(TestAbort(globals))
{ p2close((**globals).p2);
(**globals).p2 = nil;
gResult=1;
goto eexit;
}
UpdateProgress(globals,(**globals).cLine+(**globals).partial,(**globals).total);
}
switch((**globals).BitsDepth)
{
case 8:
for(i=0;i<width;i++)
*col++ = *p++;
break;
case 15:
for(i=0;i<width;i++)
{
*col = ((ulong)(p[1] >> 3) << 11); // G
*col |= ((ulong)(p[0] >> 3) << 6); // R
*col |= ((ulong)(p[2] >> 3) << 1); // B
col++;
p+=3;
}
break;
case 24:
for (i = 0; i < width; i++)
{
*col = ((ulong)p[0] << 16);
*col |= ((ulong)p[1] << 8);
*col |= (ulong)p[2];
col++;
p+=3;
}
break;
}
cLine = p2nline((**globals).p2, &(**globals).Pixel);
if(cLine==0)
{
// entering pass 2
(**globals).partial=gStuff->imageSize.v;
}
if(cLine<0)
{
if(cLine==-2)
p2close((**globals).p2);
(**globals).p2 = NULL;
SetRect(&gStuff->theRect,0,0,0,0);
gStuff->data = NULL;
return;
}
gStuff->theRect.left = 0;
gStuff->theRect.right = gStuff->imageSize.h;
gStuff->theRect.top = cLine;
gStuff->theRect.bottom = cLine+1;
(**globals).cLine = cLine;
if(gResult) goto eexit;
}
/*****************************************************************************/
/* This routine will always be called if DoStart does not return an error
(even if DoContinue returns an error or the user aborts the operation).
This allows the module to perform any needed cleanup. */
void DoFinish(GHdl globals)
{
if (gStuff->data)
gStuff->data = NULL;
}
/*****************************************************************************/
OSErr DoP2Error(GHdl globals)
{
Str255 str;
switch(p2errno)
{
case P2E_FEOF:
GetIndString(str, kStringID, kStrEOFErrItem); // EOF reached
DoError(globals,str,nil);
p2errno=1;
break;
case P2E_BADFORM:
GetIndString(str, kStringID, kStrFormatErrItem); // Format Err
DoError(globals,str,nil);
p2errno=1;
break;
default:
#if 0
// MacOS Error ... Photoshop to recover this
NumToString(p2errno,str);
DoError(globals,str,nil);
#endif
break;
}
return p2errno;
}
/* In dem Augenblick wurde mir klar,daァ ich einen schweren Fehler begangen habe. */
/*****************************************************************************/